<?php

namespace app\api\controller;

use hg\apidoc\annotation as Apidoc;
use hg\apidoc\annotation\Param;
use hg\apidoc\annotation\Query;
use hg\apidoc\annotation\Returned;
use hg\apidoc\annotation\Header;
use app\BaseController;
use think\facade\View;
use think\facade\Db;
use think\response\Json;
/**
 * 文章违禁词
 *
 * api-ArticleForbiddenwords
 *
 * @Apidoc\Title("文章违禁词")
 * @Apidoc\Group("api")
 */
class ArticleForbiddenwords extends Auth
{
    /**
     * 获取违禁词名称列表
     *
     * api-ArticleForbiddenwords-getForbiddenName
     *
     * NotHeaders
     * @Apidoc\Title("获取违禁词名称列表")
     * @Apidoc\Url("/api/ArticleForbiddenwords/getForbiddenName")
     * @Apidoc\Method("get")
     * @Returned("name", type="varchar",desc="违禁词分类名称 ")
     */
    public function getForbiddenName()
    {
        $where[] = ['member_id', '=', $this->request->uid];

        $res = \app\common\model\ArticleForbiddenwords::order("id desc")
            ->field("id,name")
            ->where($where)->select();
        $res = $res->toArray();

        return success('ok', $res);
    }


    /**
     * list
     * @Apidoc\Title("列表")
     * @return Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function list()
    {
        $where[] = ['member_id', '=', $this->request->uid];

        $res = \app\common\model\ArticleForbiddenwords::order("id desc")
            ->where($where)->select();

        return success('ok', $res);
    }


    /**
     * add
     * @Apidoc\Title("增加or编辑违禁词分类")
     */
    public function add()
    {
        $data = input();
        validate([
            'name|名称' => 'require',
            'content|违禁词' => 'require'
        ])->check($data);
        $data['member_id'] = $this->request->uid;
        if($data['id']){

            \app\common\model\ArticleForbiddenwords::update($data);

        }else{
            unset($data['id']);
            \app\common\model\ArticleForbiddenwords::create($data);
        }
        return success();
    }

    /**
     * delete
     * @Apidoc\Title("删除违禁词分类")
     */
    public function delete()
    {
        $id = input('id');
        $article_class = \app\common\model\ArticleForbiddenwords::find($id);
        if($article_class['member_id'] != $this->request->uid){
            return error('非法请求');
        }
        \app\common\model\ArticleForbiddenwords::destroy($id);
        return success();
    }
}
